package com.ophiux.FeverManagement.aplication.serviceImpl.patient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.FeverManagement.aplication.bean.patient.AddPatientInfoRequest;
import com.ophiux.FeverManagement.aplication.bean.patient.AddPatientInfoResponse;
import com.ophiux.FeverManagement.aplication.service.patient.IAddPatientInfoService;
import com.ophiux.FeverManagement.domain.entity.RbPatientinfo;
import com.ophiux.FeverManagement.domain.repository.IPatientRepositories;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.common.DateUtil;
import com.ophiux.utils.common.IDCardUtil;
@Service
public class AddPatientInfoServiceImpl implements IAddPatientInfoService{
	@Autowired
	IPatientRepositories patientRepositories;
	
	@Override
	public Result<AddPatientInfoResponse> AddPatientInfo(AddPatientInfoRequest dto) {
		Result<AddPatientInfoResponse> result = new Result<>();
		if(!dto.isParamValid()) {
			return Result.fail(ResultEnum.requestParamFail);
		}
		try {
			RbPatientinfo queryPatient = patientRepositories.getPatientByIdNo(dto.getID_NO());
			if(queryPatient != null) {//该患者存在档案信息  不允许重复建档
				return Result.fail(ResultEnum.PATIENT_EXIST);
			}
			
			RbPatientinfo rbPatient = new RbPatientinfo();
			rbPatient.setADD_TYPE(dto.getADD_TYPE());
			rbPatient.setADDRESS(dto.getADDRESS());
			rbPatient.setADMIN(dto.getADMIN());
			rbPatient.setCREATE_TIME(DateUtil.getCurDateStr());
			rbPatient.setID_NO(dto.getID_NO());
			rbPatient.setPATIENT_NAME(dto.getPATIENT_NAME());
			rbPatient.setTEL(dto.getTEL());
			
			rbPatient.setSEX(IDCardUtil.getSex(dto.getID_NO()));
			String patientId = patientRepositories.save(rbPatient);
			
			AddPatientInfoResponse response = new AddPatientInfoResponse();
			response.setPATIENT_ID(patientId);
			response.setPATIENT_NAME(dto.getPATIENT_NAME());
			
			result = Result.success(response);
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.fail("","操作异常");
		}
		return result;
	}

}
